import * as common from './common.js';
import * as db from './db.js'
import Request from '../js_sdk/luch-request/luch-request/index.js' // 下载的插件

const http = new Request();
import {
	apiBaseUrl
} from './config.js'

// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'addPraise',
	'addInfoComm',
	'addFollow',
	'addInfo',
	'getLikeList',
	'getCommList',
	'getCollectionList'
];


let loadingCount = 0;

function addLoading() {
	if (loadingCount == 0) {
		uni.showLoading({
			title: '加载中'
		});
	}
	loadingCount++
}

function isCloseLoading() {
	loadingCount--
	if (loadingCount == 0) {
		uni.hideLoading();
	}
}

const post = (url, data, Loading = 'true') => {
	if (Loading) {
		addLoading()
	}

	let header = {
		'Accept': 'application/json',
		'Content-Type': 'application/json'
		// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
	};

	if (methodsToken.indexOf(url) >= 0) {
		// 判断token是否存在
		// 获取用户token
		let token = db.get("token");
		if (!token) {
			common.jumpToLogin();
			return false;
		} else {
			header.token = token;
		}
	} else {
		let token = db.get("token");
		if (token) {
			header.token = token;
		}
	}


	return http.post(apiBaseUrl + url, data, {
		header: header,
		withCredentials: true
	}).then((response) => {
		if (Loading) {
			isCloseLoading()
		}
		const result = response.data;
		return result
	}).catch(error => {
		if (Loading) {
			isCloseLoading()
		}
		if (error && error.response) {
			return error.response
		}
	})
}

// complete

const get = (url) => {
	uni.showLoading({
		title: '加载中'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: apiBaseUrl + url,
			header: {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			},
			method: 'GET',
			withCredentials: true,
			success: (response) => {
				uni.hideLoading();
				resolve(response.data)
			},
			fail: (error) => {
				uni.hideLoading();
				if (error && error.response) {
					reject(error.response);
				}
			},
			complete: () => {
				setTimeout(function() {
					uni.hideLoading();
				}, 250);
			}
		});
	})

}
/**
 * 所有的请求
 */
//获取首页轮播图
export const getSwiperList = () => get('getSwiperList');
//用户注册
export const register = (data) => post('register', data);
//用户登录
export const login = (data) => post('login', data)
//首页获取帖子
export const getInfoList = (data) => post('getInfoList', data)
//获取帖子详情
export const infoDetail = (data) => post('infoDetail', data)
//帖子点赞
export const addPraise = (data) => post('addPraise', data)
//帖子评论
export const addInfoComm = (data) => post('addInfoComm', data)
//获取用户评论
export const getInfoComm = (data) => post('getInfoComm', data)
//获取文章
export const getKnowList = (data) => post('getKnowList', data)
//收藏
export const addFollow = (data) => post('addFollow', data)
//文章详情
export const knowDetail = (data) => post('knowDetail', data)
//文章评论
export const getKnowComm = (data) => post('getKnowComm', data)
//发起评论
export const addKnowComm = (data) => post('addKnowComm', data)
//添加帖子
export const addInfo = (data) => post('addInfo', data)
//获取点赞
export const getLikeList = (data) => post('getLikeList', data)
// 获取评论
export const getCommList = (data) => post('getCommList', data)
//获取我的收藏
export const getCollectionList = (data) => post('getCollectionList', data)

export {
	apiBaseUrl
};
